﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	infoDialog9
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;


namespace Map_Fvg
{
    public partial class infoDialog9 : Form
    {
        public infoDialog9()
        {
            InitializeComponent();

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from classi";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            listBox1.Items.Add(reader.GetString(0).ToString() + " - " + reader.GetString(1).ToString());
                        }
                    }
                    conn.Close();
                }
            }
        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s1 = textBox1.Text.Trim();
            s1 = s1.ToUpper();
            int length = s1.Length;
            if (length < 7)
            {
                MessageBox.Show("completare il codice sottoclasse");
                return;
            }

            string s2 = textBox2.Text;
            if (s2.Length == 0)
            {
                MessageBox.Show("manca specificazione proprietà");
                return;
            }

            string s3 = textBox3.Text;
            if (s3.Length == 0)
            {
                MessageBox.Show("indicare bordo");
                return;
            }

            string s4 = textBox4.Text;      // campitura
            string s6 = textBox6.Text;      //  codice bordo
            string s7 = textBox7.Text;      //  codice campitura


            Color icol = textBox5.BackColor;

            string s5 = icol.A.ToString();

            s5 = string.Concat(s5, " ", icol.R.ToString(), " ", icol.G.ToString(), " ", icol.B.ToString());


            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from area where codice_area = '" + s1 + "'";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        MessageBox.Show("esiste già un altro elemento con lo stesso codice area " + s1);
                        return;
                    }
                    conn.Close();
                }
            }

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "insert into area values('" + s1 + "','" + s2 + "','" + s7 + "','" + s4 + "','" + s6 + "','" + s3 + "','" + s5 + "')";

                    cmd.CommandText = ss;
                    cmd.ExecuteNonQuery();

                }
                conn.Close();
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog();
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox5.BackColor = colorDialog1.Color;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void infoDialog91_Load(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            string s1;
            string ss = listBox1.Text;
            if (ss.Length > 0)
            {
                s1 = ss.Substring(0, 1);
                s1 = string.Concat(s1, "A");

            }
            else
            {
                s1 = "1A";
            }
            textBox1.Text = s1;
        }

        private void listBox1_MouseEnter(object sender, EventArgs e)
        {
            Size sz = listBox1.Size;
            sz.Height = 140;
            listBox1.Size = sz;

        }

        private void listBox1_MouseLeave(object sender, EventArgs e)
        {
            Size sz = listBox1.Size;
            sz.Height = 20;
            listBox1.Size = sz;

        }






    }
}
